<!--
 * @描述: 我的消息
 * @版权所有: 广东国星科技有限公司
 * @商业授权: www.mscodecloud.com
-->
<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" :placeholder="$t('notification.title')" clearable class="filter-input filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.content" :placeholder="$t('notification.content')" clearable class="filter-input filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.type" :placeholder="$t('notification.type')" clearable class="filter-input filter-item">
        <el-option v-for="item in typeOptions" :key="item.value" :label="item.name" :value="item.value" />
      </el-select>
      <el-button v-waves type="primary" class="filter-item" icon="el-icon-search" @click="handleFilter">
        {{ $t('common.query') }}
      </el-button>
      <el-button v-waves type="primary" class="filter-item" icon="el-icon-refresh-right" @click="handleReset">
        {{ $t('common.reset') }}
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      ref="multipleTable"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40" />
      <el-table-column :label="$t('notification.title')" prop="title" />
      <el-table-column :label="$t('notification.type')" prop="typeCn" />
      <el-table-column :label="$t('notification.priority')" prop="priorityCn" />
      <el-table-column :label="$t('notification.publisher')" prop="publisher" />
      <el-table-column :label="$t('notification.create-time')" min-width="100px" prop="createTime" sortable="custom" :class-name="getSortClass('createTime')">
        <template slot-scope="{row}">
          <span>{{ row.createTime | formatDate }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.operate')" align="center">
        <template slot-scope="{row}">
          <el-button type="text" @click="handleView(row)">
            <i class="el-icon-view" />{{ $t('common.view') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-form ref="dataForm" :model="temp">
      <el-dialog v-el-drag-dialog :title="temp.title" :visible.sync="dialogFormVisible" width="850px">
        <el-row>
          <el-col :span="6">{{ $t('notification.publisher') }}：{{ temp.publisher }}</el-col>
          <el-col :span="18">{{ $t('notification.create-time') }}：{{ temp.createTime | formatDate }}</el-col>
        </el-row>
        <div style="overflow-x: auto; max-height: 500px" v-html="temp.content" />
      </el-dialog>
    </el-form>
  </div>
</template>

<script>
import { queryMyNotification, queryNotificationType } from '@/api/admin'
import waves from '@/directive/waves' // waves directive
import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import Sortable from 'sortablejs'

export default {
  name: 'MyNotification',
  components: { Pagination },
  directives: { waves, elDragDialog },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        currentPage: 1,
        pageSize: 10,
        title: undefined,
        content: undefined,
        type: undefined,
        sorter: 'createTime_descend'
      },
      multipleSelection: [],
      typeOptions: [],
      temp: {},
      dialogFormVisible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      queryMyNotification(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.pagination.total
        this.listLoading = false
      })
      queryNotificationType().then(response => {
        this.typeOptions = response.data
      })
      this.$nextTick(() => {
        this.setSort()
      })
    },
    setSort() {
      const el = this.$refs.multipleTable.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
      this.sortable = Sortable.create(el, {
        ghostClass: 'sortable-ghost', // Class name for the drop placeholder,
        setData: function(dataTransfer) {
          // to avoid Firefox bug
          dataTransfer.setData('Text', '')
        },
        onEnd: evt => {
          const targetRow = this.list.splice(evt.oldIndex, 1)[0]
          this.list.splice(evt.newIndex, 0, targetRow)
        }
      })
    },
    handleFilter() {
      this.listQuery.currentPage = 1
      this.getList()
    },
    handleReset() {
      this.listQuery.title = ''
      this.listQuery.content = ''
      this.listQuery.type = ''
      this.handleFilter()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'createTime') {
        this.sortByCreateTime(order)
      }
    },
    sortByCreateTime(order) {
      if (order === 'ascending') {
        this.listQuery.sorter = 'createTime_ascend'
      } else {
        this.listQuery.sorter = 'createTime_descend'
      }
      this.handleFilter()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    getSortClass: function(key) {
      const sorter = this.listQuery.sorter
      return sorter === `${key}_ascend` ? 'ascending' : 'descending'
    },
    handleView(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogFormVisible = true
    }
  }
}
</script>
